/**
 * @class LastFM
 * @description Class to connect with the LastFM PHP Service with AJAX calls
 */
var LastFM = function()
{
	var webserviceURL = "http://localhost/mwm04-html5/php/webservice.php";

	/**
	 * Get a user by his username
	 *
	 * @param username string
	 * @param friendsCount int
	 * @param artistsCount int
	 */
	this.getUserByName = function(username, friendsCount, artistsCount)
	{
		$.ajax({
            url: webserviceURL+"?op=userGet&username="+username,
            success: function (data)
			{
                //Create & Initalise new User
				var user = new User();
				user.init(data.user);
				initNewUser(user);

				//Add friends & artist to Object
				user.getFriends(friendsCount);
				user.getArtists(artistsCount);
				
				//Add user to DB if not exists
				dbActions.addUser(user);
            }
        });
	}

	/**
	 * Get friends by a user
	 *
	 * @param user User
	 * @param limit int
	 */
	this.getFriendsByUser = function(user, limit)
	{
		$.ajax({
            url: webserviceURL+"?op=userFriends&username="+user.name+"&limit="+limit,
            success: function (data)
			{
				//Hide the loader & show the data after friend have been loaded
				toggleWrapperLoader();

				//Loop through result friends
				for (var i = 0; i < data.friends.user.length; i++)
				{
					//Push friend within userObject & show within the box
					user.friends.push(data.friends.user[i]);
					showFriendInBox(data.friends.user[i]);

					//Add friend to the DB if combination doensn't exist
					dbActions.addUserFriend(user.name, data.friends.user[i].name);
				}
            }
        });
	}

	/**
	 * Get Artists by User
	 *
	 * @param user User
	 * @param limit int
	 */
	this.getArtistsByUser = function(user, limit)
	{
		$.ajax({
            url: webserviceURL+"?op=userArtist&username="+user.name+"&limit="+limit,
            success: function (data)
			{
                //Loop through the artists
				for (var i = 0; i < data.artists.artist.length; i++)
				{
					//Create new artist & getAlbums
					var newArtist = new Artist();
					newArtist.init(data.artists.artist[i]);
					newArtist.getAlbums();

					//Push artist into UserObject
					user.artists.push(newArtist);

					//Add this artist to the DB if not already exists
					dbActions.addArtist(newArtist);

					//Add user/artist combination if not already exists
					dbActions.addArtistByUser(user.id, newArtist.id);
				}
            }
        });
	}

	/**
	 * Get topAlbums from the given artist
	 *
	 * @param artist Artist
	 */
	this.getArtistTopAlbums = function(artist)
	{
		$.ajax({
            url: webserviceURL+"?op=artistAlbums&artistName="+artist.name+"&id="+artist.id,
            success: function (data)
			{
				//Loop through the albums
				for (var i = 0; i < data.topalbums.album.length; i++)
				{
					//Create new Album
					var album = new Album();
					album.init(data.topalbums.album[i]);

					//Push album into ArtistObject
					artist.albums.push(album);

					//Add the album to the DB if not exists
					dbActions.addAlbumByArtistId(artist.id, album);
				}
            }
        });
	}

	/**
	 * Compare the music taste of the two given users
	 *
	 * @param user1 string
	 * @param user2 string
	 * @param limit int
	 */
	this.compareUserTaste = function(user1, user2, limit)
	{
		$.ajax({
            url: webserviceURL+"?op=compareTaste&user1="+user1+"&user2="+user2+"&limit="+limit,
            success: function (data)
			{
				//Put the artist within Artist objects and push them in 1 Array!
				var artistArray = Array();
				for (var i = 0; i < data.comparison.result.artists.artist.length; i++)
				{
					var artist = new Artist();
					artist.init(data.comparison.result.artists.artist[i]);

					artistArray.push(artist);
				}

				//Get score for user/friend
				var score = data.comparison.result.score;

				//Add comparion to the DB if not exists
				dbActions.addUserComparions(user1, user2, score, artistArray);

				//Handle compare data
				handleUserCompareData(artistArray, user2, score);
            }
        });
	}
}